/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Contains helpers to simplify the calculation of md5 sums.
 *
 * @author John R Sohn
 */
public class MD5Helpers {

    /**
     * Calculates a jigdo Base64 MD5 from part of a byte[]
     *
     * @param src Source byte[]
     * @param start Starting index of the byte[]
     * @param length Length of byte[] part
     * @return The jigdo base64 md5 string.
     * @throws java.security.NoSuchAlgorithmException
     */
    public static String GetJigdoMD5FromPart(byte[] src, int start, int length)
            throws java.security.NoSuchAlgorithmException {
        String m64 = jigdo.utils.ByteUtils.GetJigdo64(GetMD5FromPart(src, start, length));
        return m64;
    }
    
    public static String GetJigdoMD5FromFilePart(String FileName, long offset, long length)
            throws FileNotFoundException, NoSuchAlgorithmException, IOException
    {
        return ByteUtils.GetJigdo64(GetMD5FromFilePart(FileName,offset,length));
    }

    public static byte[] GetMD5FromFilePart(String FileName, long offset, long length)
            throws FileNotFoundException, NoSuchAlgorithmException, IOException {

        RandomAccessFile rs = new RandomAccessFile(FileName, "r");

        MessageDigest md5 = MessageDigest.getInstance("MD5");

        rs.seek(offset);

        byte[] buffer = new byte[1024];

        long bytesremaining = length;

        while (bytesremaining > 0) {
            int bytesread =
                    rs.read(buffer, 0, (int)bytesremaining > 1024 ? 1024 : (int)bytesremaining);
           
            bytesremaining -= bytesread;

            byte[] temp = new byte[bytesread];

            System.arraycopy(buffer, 0, temp, 0, bytesread);

            md5.update(temp);
        }
        
        rs.close();
        
        return md5.digest();

    }

    public static String GetJigdoMD5FromFile(String fileName)
            throws NoSuchAlgorithmException, IOException {
        return ByteUtils.GetJigdo64(GetFileMD5(fileName));
    }

    public static byte[] GetFileMD5(String filename)
            throws NoSuchAlgorithmException, IOException {
        InputStream fis = new FileInputStream(filename);

        byte[] buffer = new byte[1024];

        MessageDigest complete = MessageDigest.getInstance("MD5");

        int numRead = 1;

        while (numRead > 0) {
            numRead = fis.read(buffer, 0, 1024);

            if (numRead > 0) {
                byte[] val = new byte[numRead];

                System.arraycopy(buffer, 0, val, 0, numRead);

                complete.update(val);
            }
        }

        fis.close();

        return complete.digest();
    }

    /**
     * Calculates an MD5 from part of a byte[]
     *
     * @param src Source byte[]
     * @param start Starting index of the byte[]
     * @param length Length of byte[] part
     * @return The md5 bytes.
     * @throws java.security.NoSuchAlgorithmException
     */
    public static byte[] GetMD5FromPart(byte[] src, int start, int length)
            throws java.security.NoSuchAlgorithmException {

        byte[] inputPiece = new byte[length];

        System.arraycopy(src, start, inputPiece, 0, length);

        return GetMD5(inputPiece);


    }

    /**
     * Calculates an md5 from the given byte[]
     *
     * @param src The source byte array
     * @return The md5 bytes
     * @throws java.security.NoSuchAlgorithmException
     */
    public static byte[] GetMD5(byte[] src) throws java.security.NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");

        byte[] md5 = md.digest(src);

        return md5;
    }

    /**
     * Calculates a jigdo base64 md5 from the given byte[]
     *
     * @param src The source byte array
     * @return The jigdo base64 md5 string
     * @throws java.security.NoSuchAlgorithmException
     */
    public static String GetJigdoMD5(byte[] src) throws java.security.NoSuchAlgorithmException {
        String m64 = jigdo.utils.ByteUtils.GetJigdo64(GetMD5(src));
        return m64;
    }
}
